package cn.com.darly.videoandroidpm.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.darly.dview.widget.camera.util.ImageLoaderUtil;

import java.util.ArrayList;
import java.util.List;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.common.StringMatcher;
import cn.com.darly.videoandroidpm.ui.conversation.video.VideoStartCallActivity;

/** 带标签的ListView适配器
 * @author Darly/张宇辉/2018/5/7 11:29
 * @version 1.0/cn.com.darly.videoandroidpm.ui.adapter
 */
public class FragmentFriendAdapter extends RecyclerView.Adapter<FragmentFriendAdapter.UserHolder>  implements View.OnClickListener{

    private List<LoginInfo> userList;

    private Context context;

    private OnRecycItemClickListener listener;

    private RecyclerView recyclerView;
    public FragmentFriendAdapter(Context context) {
        this.context = context;
        userList = new ArrayList<>();
    }
    public void setOnRecycItemClickListener(OnRecycItemClickListener listener) {
        this.listener = listener;
    }


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }


    @Override
    public UserHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_fragment_friend, parent, false);
        view.setOnClickListener(this);
        return new UserHolder(view);
    }

    @Override
    public void onClick(View v) {
        if (recyclerView != null && listener != null) {
            int position = recyclerView.getChildAdapterPosition(v);
            listener.OnRecycItemClick(recyclerView, v, position, userList.get(position));
        }
    }

    @Override
    public void onBindViewHolder(UserHolder holder, final int position) {
        ImageLoaderUtil.getInstance().loadImageNor("",holder.id_fragment_friend_icon);
        holder.id_fragment_friend_note.setText(userList.get(position).getName());
        holder.id_fragment_friend_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(context, VideoStartCallActivity.class);
                    intent.putExtra("LoginInfo",userList.get(position));
                    context.startActivity(intent);
                }
        });
    }

    public void setData(List<LoginInfo> userList) {
        this.userList.clear();
        this.userList = userList;
        notifyDataSetChanged();
    }

    public int getFirstPositionByChar(char sign) {
        if (sign == '#') {
            return 0;
        }
        for (int i = 0; i < userList.size(); i++) {
            String name = userList.get(i).getName();
            if (StringMatcher.getHeadChar(name) == sign){
                    return i;
            }
        }
        return -1;
    }

    @Override
    public int getItemCount() {
        return userList==null?0:userList.size();
    }

    class UserHolder extends RecyclerView.ViewHolder {

        public ImageView id_fragment_friend_icon;

        public TextView id_fragment_friend_note;

        public ImageView id_fragment_friend_video;

        public UserHolder(View itemView) {
            super(itemView);
            id_fragment_friend_icon = (ImageView) itemView.findViewById(R.id.id_fragment_friend_icon);
            id_fragment_friend_note = (TextView) itemView.findViewById(R.id.id_fragment_friend_note);
            id_fragment_friend_video = (ImageView) itemView.findViewById(R.id.id_fragment_friend_video);
        }
    }
    /**
     * 自定义RecyclerView的点击事件
     */
    public interface OnRecycItemClickListener {
        void OnRecycItemClick(RecyclerView parent, View view, int position, LoginInfo data);
    }
}